import type {SysUserAdd, SysUserData, SysUserDetail, SysUserParams} from "@/pages/system/user/type";
import {request} from "@@/plugin-request/request";
import download from "@/utils/download";
import {HttpResult, Page} from "@/services/typings";
import {LoginUser} from "@/services/system/login/types";


export const sysUserPageList = async (sysUserParams: SysUserParams) => {

  return request<HttpResult<Page<SysUserData>>>('/api/sys/user/list', {
    method: 'POST',
    data: sysUserParams
  });
}




export const sysUserUpdate = async (sysUserAdd: SysUserAdd) => {
  return request<{
    data: string;
  }>('/api/sys/user/update', {
    method: 'POST',
    data: sysUserAdd
  });
}

export const sysUserAdd = async (sysUserAdd: SysUserAdd) => {
  return request<{
    data: string;
  }>('/api/sys/user/add', {
    method: 'POST',
    data: sysUserAdd
  });
}

export const sysUserRemove = async (ids: (number | string)[]) => {
  return request<{
    data: string;
  }>('/api/sys/user/remove', {
    method: 'POST',
    data: {
      ids
    }
  });
}

export const sysUserDetail = async (id: number) => {
  return request<{
    data: SysUserDetail;
  }>('/api/sys/user/detail', {
    method: 'POST',
    data: {
      id
    }
  });
}

export const sysUserImport = async (formData: FormData) => {
  return request<{
    data: string;
  }>('/api/sys/user/import', {
    method: 'POST',
    data: formData,
    requestType: "form"
  });
}


const sysUserExport = async (ids: (number | string)[]) => {
  return request<{
    data: string;
  }>('/api/sys/user/export', {
    method: 'POST',
    data: {ids},
    responseType: 'blob',
    getResponse: true,
  });
}

export const downloadSysUserExport = async (ids: (number | string)[]) => {
  await sysUserExport(ids).then(({data, response}) => {
    download(data, response)
  });
}


const sysUserExportTemplate = async () => {
  return request<{
    data: string;
  }>('/api/sys/user/export/template', {
    method: 'POST',
    responseType: 'blob',
    getResponse: true,
  });
}

export const downloadSysUserExportTemplate = async () => {
  await sysUserExportTemplate().then(({data, response}) => {
    download(data, response)
  });
}


export const getUserInfo = async () => {
  return request<HttpResult<LoginUser>>('/api/sys/user/info', {
    method: 'POST',
  });
}




